﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Domain.Repositories;
using YAPACS.Organizations;

namespace YAPACS.RisServer.Controllers;

[Authorize]
public class DepartmentController : AbpControllerBase
{
    private readonly IRepository<Department, Guid> _departmentRepository;

    public DepartmentController(IRepository<Department, Guid> departmentRepository)
    {
        _departmentRepository = departmentRepository;
    }

    [AllowAnonymous]
    [HttpGet("/api/department/get-department-info/{departmentCode}")]
    public async Task<IActionResult> GetDepartmentInfo(string departmentCode)
    {
        try
        {
            var department = await _departmentRepository.GetAsync(e => e.Code == departmentCode);
            return Ok(new
            {
                Success = true,
                Data = new
                {
                    department.Id,
                    department.Name,
                    department.Code,
                    department.Logo,
                    department.Alias,
                    department.Alias2,
                    department.Alias3,
                    InstitutionName = department.Institution.Name,
                    InstitutionLogo = department.Institution.Logo,
                    InstitutionAlisa = department.Institution.Alias,
                    InstitutionAlisa2 = department.Institution.Alias2,
                }
            });
        }
        catch (Exception ex)
        {
            return Ok(new
            {
                Success = false,
                Error = ex.Message
            });
        }
    }
}
